package com.lmpop.updateHtml.updateHtml;

import com.lmpop.updateHtml.parseHtml.UpdateHtml;
import com.lmpop.updateHtml.parseXml.Config;
import com.lmpop.updateHtml.util.FileUtil;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Hashtable;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UpdateJsp
{
  public Config conf;
  public String ecms_path;
  private FileUtil fileUtil = new FileUtil();
  String regex_str = "[$]{1}[{]{1}[a-zA-Z_]+[}]{1}";
  public UpdateHtml updateHtml;

  public UpdateJsp()
  {
    this.conf = new Config();
    this.conf.init();
    this.ecms_path = this.conf.getString("rootpath");
    this.updateHtml = new UpdateHtml();
  }

  public void updateWeb(String index_temp, String save_dir, String default_page, String param_file_path, Hashtable paramMap)
  {
    String temp_file = this.ecms_path + index_temp;
    String inStr = this.fileUtil.getContentFromFile(temp_file);
    Map proMap = getProMap(this.ecms_path + param_file_path);
    Pattern p = Pattern.compile(this.regex_str);
    Matcher m = p.matcher(inStr);
    String proKey = "";
    while (m.find()) {
      proKey = m.group();
      if (proMap.get(proKey) != null) {
        String value = proMap.get(proKey).toString();
        value = value.replace(";", ";\n");
        inStr = inStr.replace(proKey, value);
      }
    }

    inStr = this.updateHtml.updateHtml(inStr, paramMap);

    String html_file = this.ecms_path + "/" + save_dir + "/" + default_page;
    String index_path = this.ecms_path + "/" + save_dir + "/";
    saveToFile(html_file, index_path, inStr);
  }

  public void saveToFile(String html_file, String index_path, String tempStr) {
    this.fileUtil.makeDir(index_path);
    this.fileUtil.saveToFile(tempStr, html_file);
    System.out.println("create file successfully!");
  }

  public Map getProMap(String fieldspath)
  {
    Map proMap = new Hashtable();
    try
    {
      FileReader ffield = new FileReader(fieldspath);
      BufferedReader fieldbuff = new BufferedReader(ffield);
      String fieldline = fieldbuff.readLine();

      while (fieldline != null)
      {
        fieldline = fieldline.trim();
        if (!fieldline.equals(""))
        {
          String key = proKey(fieldline);
          String value = fieldline.replace(key + "=", "");
          proMap.put(key, value);
        }
        fieldline = fieldbuff.readLine();
      }

      fieldbuff.close();
      fieldbuff = null;
      ffield.close();
      ffield = null;
    }
    catch (IOException e)
    {
      throw new RuntimeException("read file error:" + fieldspath);
    }
    return proMap;
  }

  public String proKey(String readLine) {
    Pattern p = Pattern.compile(this.regex_str);
    Matcher m = p.matcher(readLine);
    String proKey = "";
    while (m.find()) {
      proKey = m.group();
    }
    return proKey;
  }
}